//
//  ALSingleTon.m
//  FanBei
//
//  Created by 哀木涕 on 2016/12/27.
//  Copyright © 2016年 chuxiangkeji. All rights reserved.
//

#import "ALSingleTon.h"

@implementation ALSingleTon
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (instancetype)shareSingTon
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

- (BOOL)isLogin{
    return self.userInfo.mj_keyValues.count;
}


#pragma mark 用户信息处理

- (void)setUserInfo:(ALUserInfo *)userInfo{
    [userInfo.mj_keyValues writeToFile:[self localFilePathName] atomically:YES];
}
- (ALUserInfo *)userInfo{
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[self localFilePathName]];
    return [ALUserInfo mj_objectWithKeyValues:dic];
}
- (NSString*)localFilePathName
{
    NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [docDir stringByAppendingPathComponent:@"userInfo.plist"];
}

//清除用户信息
- (BOOL)cleanUserInfo{
   return [[NSFileManager defaultManager] removeItemAtPath:[self localFilePathName] error:nil];
}
//保存用户信息
- (BOOL)saveUserInfo{
    if ([[self localFilePathName] isEmptyString]){
        [[NSFileManager defaultManager] createFileAtPath:[self localFilePathName] contents:nil attributes:nil];
        
    }
    NSLog(@"%@",[self localFilePathName]);
    return [self.userInfo.mj_keyValues writeToFile:[self localFilePathName] atomically:YES];
}



@end
